﻿using log4net;
using log4net.Config;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace YbxLibrary
{
    public  class LogHelper
    {
        private static readonly ILog Log;
        static LogHelper()
        {
            XmlConfigurator.ConfigureAndWatch(new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "Log4net.config"));
            Log = LogManager.GetLogger(typeof(LogHelper));
        }
        /// <summary>
        /// 记录调试信息
        /// </summary>
        /// <param name="message">信息</param>
        public static void Debug(object message)
        {
            Log.Debug(getRerodMsg(message));
        }
        /// <summary>
        /// 记录警告信息
        /// </summary>
        /// <param name="message">信息</param>
        public static void Warn(object message)
        {
            Log.Warn(getRerodMsg(message));
        }
        /// <summary>
        /// 记录错误信息
        /// </summary>
        /// <param name="message">信息</param>
        public static void Error(object message)
        {
            Log.Error(getRerodMsg(message));
        }
        /// <summary>
        /// 记录重要提示信息
        /// </summary>
        /// <param name="message">信息</param>
        public static void Info(object message)
        {
             //Log.Info(message);
             Log.Info(getRerodMsg(message));
        }
        /// <summary>
        /// 记录信息和异常信息
        /// </summary>
        /// <param name="message">错误信息</param>
        /// <param name="ex">异常对象</param>
        public static void Debug(object message, Exception ex)
        {
            Log.Debug(getRerodMsg(message), ex);
        }
        /// <summary>
        /// 记录信息和异常信息
        /// </summary>
        /// <param name="message">错误信息</param>
        /// <param name="ex">异常对象</param>
        public static void Warn(object message, Exception ex)
        {
            Log.Warn(getRerodMsg(message), ex);
        }
        /// <summary>
        /// 记录信息和异常信息
        /// </summary>
        /// <param name="message">错误信息</param>
        /// <param name="ex">异常对象</param>
        public static void Error(object message, Exception ex)
        {
            Log.Error(getRerodMsg(message), ex);
        }
        /// <summary>
        /// 记录信息和异常信息
        /// </summary>
        /// <param name="message">错误信息</param>
        /// <param name="ex">异常对象</param>
        public static void Info(object message, Exception ex)
        {
            Log.Info(getRerodMsg(message), ex);
        }

        private static int getRecordLen()
        {
            string currentDirectory = AppDomain.CurrentDomain.BaseDirectory;
            IniFileUtils iniFile = new IniFileUtils(currentDirectory + "yb.ini");
            return Convert.ToInt32(iniFile.ReadValue("log", "len"));
        }

        private static String getRerodMsg(Object message)
        {
            String msg = Convert.ToString(message);
            int len = getRecordLen();
            if (len < 0)
            {
                return msg;
            }
            else if (len==0) {
                return msg.Length > len ? msg.Substring(0, 1) : msg;
            }
            else
            {
                return msg.Length > len ? msg.Substring(0, len) : msg;
            }
        }
    }
}
